﻿using EmbeddedSharp.Core;

namespace Zove.Runtime.Core.Expression {

    /// <summary>
    /// Zove expression parser
    /// </summary>
    public class ZoomExpressionParser : ExpressionParser {

        /// <summary>
        /// Runtime model
        /// </summary>
        protected RuntimeModel rm;

        /// <summary>
        /// Constructor with expression and the runtime model
        /// </summary>
        /// <param name="expr">expression</param>
        /// <param name="rm">runtime model</param>
        public ZoomExpressionParser(string expr, RuntimeModel rm)
            : base(expr) {
            this.rm = rm;
        }

        /// <summary>
        /// Get a variable
        /// </summary>
        /// <param name="value">the variable name</param>
        /// <returns>the variable node</returns>
        public override Node GetVariableNode(string value) {
            string varName = value;
            //if (value.Contains(".")) {
            //    string instanceName = value.Split('.')[0];
            //    varName = value.Split('.')[1];
            //}

            var fn = new FactorNode
                         {
                             Factor = rm.Factors[varName]
                         };
            if (fn.Factor == null) {
                throw new FactorNotDeclaredException(
                    value, base.lexer.LineNumber, rm.Name);
            }

            return fn;
        }
    }
}